﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Arztmodul
{
    /// <summary>
    /// Version: 1.2
    ///  Author: Jan Baltzer
    ///    Date: 2012-06-29
    /// mainForm with menu
    /// </summary>
    public partial class Menu : Form
    {
        //declare HCSBackEnd 
        private IPersistDoc persi;

        /// <summary>
        /// constructor to initialize menu
        /// </summary>
        /// <param name="persi">the concrete backEnd to work with</param>
        public Menu(IPersistDoc persi)
        {
            InitializeComponent();

            //public hcsbackend for class
            this.persi = persi;

            //try to connect
            connectButton_Click(null, null);
        }

        /// <summary>
        /// disable buttons and textfields and show connect-button if database fail
        /// </summary>
        private void resetButtons()
        {
            //disable Buttons
            this.showButton.Enabled = false;
            this.editButton.Enabled = false;
            this.createButton.Enabled = false;

            //disable TextFields
            this.firstNameField.Enabled = false;
            this.nameField.Enabled = false;

            //show Connection Button
            this.connectButton.Show();
        }

        /// <summary>
        /// routine to handel exitButton_Click event
        /// </summary>
        /// <param name="sender">sender of event</param>
        /// <param name="e">event</param>
        private void exitButton_Click(object sender, EventArgs e)
        {
            Close();
        }

        /// <summary>
        /// routine to handel showButton_Click event
        /// </summary>
        /// <param name="sender">sender of event</param>
        /// <param name="e">event</param>
        private void showButton_Click(object sender, EventArgs e)
        {
            //check dataBase
            if (this.persi.connect())
            {
                Patient patient;

                //get patient if exist
                if (persi.patientExists(this.nameField.Text, this.firstNameField.Text))
                {
                    patient = this.persi.getPatient(this.nameField.Text, this.firstNameField.Text);
                    
                    //show new form
                    new Show(patient, persi).Show();
                }
                else
                {
                    //show warning
                    MessageBox.Show(HCSWarnings.MESSAGE_BOX_WARNING_NO_PATIENT, HCSWarnings.MESSAGE_BOX_WARNING_CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            else
            {
                //show warning
                MessageBox.Show(HCSWarnings.MESSAGE_BOX_WARNING_NO_DATABASE, HCSWarnings.MESSAGE_BOX_WARNING_CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        /// <summary>
        /// routine to handel connectButton_Click event
        /// </summary>
        /// <param name="sender">sender of event</param>
        /// <param name="e">event</param>
        private void connectButton_Click(object sender, EventArgs e)
        {
            //try to connect to DataBase and enable Menu if successfull
            if (this.persi.connect())
            {
                //enable Buttons
                this.showButton.Enabled = true;
                this.editButton.Enabled = true;
                this.createButton.Enabled = true;

                //enable TextFields
                this.firstNameField.Enabled = true;
                this.nameField.Enabled = true;

                //hide Connection Button
                this.connectButton.Hide();
            }
            else 
            {
                //show warning
                MessageBox.Show(HCSWarnings.MESSAGE_BOX_WARNING_NO_DATABASE, HCSWarnings.MESSAGE_BOX_WARNING_CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        /// <summary>
        /// routine to handel editButton_Click event
        /// </summary>
        /// <param name="sender">sender of event</param>
        /// <param name="e">event</param>
        private void editButton_Click(object sender, EventArgs e)
        {
            Patient patient;
            
            //check dataBase
            if (persi.connect())
            {

                //get patient if exist
                if (persi.patientExists(this.nameField.Text, this.firstNameField.Text))
                {
                    patient = this.persi.getPatient(this.nameField.Text, this.firstNameField.Text);

                    //show new form
                    new EditCreate(patient, persi).ShowDialog();
                }
                else
                {
                    //show warning
                    MessageBox.Show(HCSWarnings.MESSAGE_BOX_WARNING_NO_PATIENT, HCSWarnings.MESSAGE_BOX_WARNING_CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            else 
            {
                //show warning
                MessageBox.Show(HCSWarnings.MESSAGE_BOX_WARNING_NO_DATABASE, HCSWarnings.MESSAGE_BOX_WARNING_CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        /// <summary>
        /// routine to handel createButton_Click event
        /// </summary>
        /// <param name="sender">sender of event</param>
        /// <param name="e">event</param>
        private void createButton_Click(object sender, EventArgs e)
        {
            Patient patient = new Patient(this.nameField.Text, this.firstNameField.Text);

            //check dataBase
            if (persi.connect())
            {
                //check if patient is new
                if (!(this.persi.patientExists(patient)))
                {
                    //show new form
                    new EditCreate(patient, persi).ShowDialog();
                }
                else
                {
                    //show warning
                    MessageBox.Show(HCSWarnings.MESSAGE_BOX_WARNING_PATIENT, HCSWarnings.MESSAGE_BOX_WARNING_CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            else
            {
                //show warning
                MessageBox.Show(HCSWarnings.MESSAGE_BOX_WARNING_NO_DATABASE, HCSWarnings.MESSAGE_BOX_WARNING_CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
    }
}
